package model;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileInputStream;
import java.util.Map;
import java.util.Scanner;
import java.util.TreeMap;
import java.util.Vector;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class FoodServiceBudget extends Budget{
	private Vector<Restaurant> restaurants;
	
	public FoodServiceBudget(String department,String file){
		super(file);
		this.setDepartment(department);
		
		restaurants = new Vector<Restaurant>();
		
		loadData(file);
	}
	
	public void showInfo(Container parent){
		FoodServiceDialog s = new FoodServiceDialog(parent);
		s.setVisible(true);
	}
	
	private void loadData(String file){
		try {
			Scanner scanner = new Scanner(new FileInputStream(file));
		
			this.setBudget(Double.parseDouble(scanner.nextLine()));
			
			int totalNumEmployees = 0;
			double totalSalary = 0;
			double totalMaintenance = 0;
			double totalSales = 0;
			double totalSupplies = 0;
			
			while(scanner.hasNext()){
				Restaurant r = new Restaurant();
				String line = scanner.nextLine();
				r.setName(line);
				
				line = scanner.nextLine();
				String[] emp = line.split(",");
				
				int numEmps = Integer.parseInt(emp[0]);
				double salary = Double.parseDouble(emp[1]);
				totalNumEmployees += numEmps;
				totalSalary += salary;
				
				r.setNumberOfEmployees(numEmps);
				r.setSalary(salary);
				
				
				line = scanner.nextLine();
				String[] supplies = line.split(",");
				for(int i=0; i<supplies.length; i+=2){
					r.addSupply(supplies[i], Double.valueOf(supplies[i+1]));
				}
				
				line = scanner.nextLine();
				String[] maintenance = line.split(",");
				for(int i=0; i<maintenance.length; i+=2){
					r.addMaintenance(maintenance[i], Double.valueOf(maintenance[i+1]));
				}
				
				line = scanner.nextLine();
				String[] sales = line.split(",");
				for(int i=0; i<sales.length; i+=2){
					r.addSale(sales[i], Double.valueOf(sales[i+1]));
				}
				
				totalSales += r.getTotalSales();
				totalMaintenance += r.getTotalMaintenance();
				totalSupplies += r.getTotalSupplies();
				
				restaurants.add(r);
			}
			
			this.setNumberOfEmployees(totalNumEmployees);
			this.setTotalSalary(totalSalary);
			this.setExpenses(totalSupplies + totalSalary + totalMaintenance);
			this.setProfit(totalSales);
			this.setTotal(this.getProfit() - this.getExpenses());
		}
		catch(Exception e){
			e.printStackTrace();
		}
	}
	
	private String[] getRestaurantFilterNames(){
		String[] names = new String[restaurants.size() + 1];
		names[0] = "All";
		for(int i=0; i<restaurants.size(); i++){
			names[i+1] = restaurants.get(i).getName();
		}
		return names;
	}
	
	private class FoodServiceDialog extends JDialog{
		private Container parent;
		private JTextArea text;
		private JComboBox filter;
		
		public FoodServiceDialog(Container parent){
			this.parent = parent;
			initGUI();
		}
		private void initGUI(){
			JPanel panel = new JPanel(new BorderLayout());
			filter = new JComboBox(getRestaurantFilterNames());
			filter.addActionListener(new ComboHandler());
			panel.add(filter,BorderLayout.NORTH);
			panel.add(new JButton(new ButtonAction("Close")),BorderLayout.SOUTH);
			
			
			
			String str = getFilteredText((String)filter.getSelectedItem());
			
			text = new JTextArea(str,10,20);
			text.setEditable(false);
			JScrollPane scroll = new JScrollPane(text);
			scroll.setPreferredSize(new Dimension(600,700));
			panel.add(scroll,BorderLayout.CENTER);
			//panel.add(new JPanel(),BorderLayout.WEST);
			//panel.add(new JPanel(),BorderLayout.EAST);
			this.add(panel);
		
			this.pack();
			this.setModal(true);
			int w = getWidth();
			int h = getHeight();
			Dimension screenSize = parent.getSize();
			int screenWidth = screenSize.width;
			int screenHeight = screenSize.height;
			int x = (screenWidth - w)/2;
			int y = (screenHeight - h)/2 + 150;
			setLocation(x,y);
		}
		
		private String getFilteredText(String filter){
			String str = "";
			for(int i=0;i<restaurants.size();i++){
				if(filter.equals("All") || filter.equals(restaurants.get(i).getName())){
					str += restaurants.get(i).toString() + "\n\n";
				}
			}
			
			double totalExpenses = 0;
			for(int i=0;i<restaurants.size();i++){
				if(filter.equals("All") || filter.equals(restaurants.get(i).getName())){
					totalExpenses += restaurants.get(i).getTotalMaintenance();
					totalExpenses += restaurants.get(i).getTotalSupplies();
					totalExpenses += restaurants.get(i).getSalary();
				}
			}
			
			double totalSales = 0;
			for(int i=0;i<restaurants.size();i++){
				if(filter.equals("All") || filter.equals(restaurants.get(i).getName())){
					totalSales += restaurants.get(i).getTotalSales();
				}
			}
			
			double profit = totalSales - totalExpenses;
			
			if(filter.equals("All")){
				str += "\n";
				str += "Total expenses:\t" + Double.toString(totalExpenses);
				str += "\nTotal sales:\t" + Double.toString(totalSales);
				str += "\nNet profit:\t" + Double.toString(profit);
			}
			return str;
		}
		private class ComboHandler implements ActionListener{

			public void actionPerformed(ActionEvent e) {
				String f = (String)filter.getSelectedItem();
				String s = getFilteredText(f);
				text.setText(s);
				repaint();
			}
			
		}
		private class ButtonAction extends AbstractAction{
			/**
			 * 
			 */
			private static final long serialVersionUID = 1L;

			public ButtonAction(String name) {
				putValue(Action.NAME, name); 
			}
			
			public void actionPerformed(ActionEvent event) {
				if(this.getValue(Action.NAME).equals("Close")){
					FoodServiceDialog.this.setVisible(false);
				}
			}
		}
	}
	private class Restaurant{
		private String name;
		private int numberOfEmployees;
		private double salary;
		private Map<String,Double> sales;
		private Map<String,Double> supplies;
		private Map<String,Double> maintenance;
		
		public double getTotalSales(){
			double totalSales = 0;
			for(String key : sales.keySet() ){
				totalSales += sales.get(key);
			}
			return totalSales;
		}
		
		public double getTotalMaintenance(){
			double totalMaintenance = 0;
			for(String key : maintenance.keySet() ){
				totalMaintenance += maintenance.get(key);
			}
			return totalMaintenance;
		}
		
		public double getTotalSupplies(){
			double totalSupplies = 0;
			for(String key : supplies.keySet() ){
				totalSupplies += supplies.get(key);
			}
			return totalSupplies;
		}
		
		public double getSalary(){
			return salary;
		}
		public void setSalary(double sal){
			this.salary = sal;
		}
		public String getName() {
			return name;
		}

		public void setName(String name) {
			this.name = name;
		}

		public int getNumberOfEmployees() {
			return numberOfEmployees;
		}

		public void setNumberOfEmployees(int numberOfEmployees) {
			this.numberOfEmployees = numberOfEmployees;
		}

		public Restaurant(){
			sales = new TreeMap<String,Double>();
			supplies = new TreeMap<String,Double>();
			maintenance = new TreeMap<String,Double>();
		}
		
		public void addSale(String name, double value){
			sales.put(name, value);
		}
		public void addSupply(String name, double value){
			supplies.put(name, value);
		}
		public void addMaintenance(String name, double value){
			maintenance.put(name,value);
		}
		
		public String toString(){
			String s = "";
			s += name + "\n";
			s += "\n\tNumber of employeees: " + Integer.toString(numberOfEmployees) + "\n";
			s += "\n";
			s += "\tExpenses:\n";
			s += "\t\tSalary:\n";
			s += "\t\t\tSalary\t\t" + Double.toString(salary) + "\n";
			s += "\n\t\t\tTotal: " + Double.toString(salary) + "\n";
			
			s += "\n\t\tSupplies:\n";
			
			double metaTotal = -salary;
			double total = 0;
			for(String key : supplies.keySet()){
				total += supplies.get(key);
				s += "\t\t\t" + key + "\t\t" + supplies.get(key) + "\n";
			}
			s += "\n\t\t\tTotal: " + Double.toString(total) + "\n";
			metaTotal -= total;
			
			s += "\n\t\tMaintenance:\n";
			
			total = 0;
			for(String key : maintenance.keySet() ){
				total += maintenance.get(key);
				s += "\t\t\t" + key + "\t\t" + maintenance.get(key) + "\n";
			}
			
			s += "\n\t\t\tTotal: " + Double.toString(total) + "\n";
			metaTotal -= total;
			
			s += "\n\t\tTotal expenses: " + Double.toString(metaTotal) + "\n";
			
			s += "\n\tSales:\n";
			
			total = 0;
			for(String key : sales.keySet() ){
				total += sales.get(key);
				s += "\t\t" + key + "\t\t" + sales.get(key) + "\n";
			}
			
			s += "\n\t\tTotal sales: " + Double.toString(total) + "\n";
			metaTotal += total;
			
			s += "\n\tNet profit: " + Double.toString(metaTotal) + "\n";
			
			return s;
		}
	}
	
}
